require.config({baseUrl: '/js/lib',urlArgs: "v0.0.1"});
require(['zepto', 'lib'], function($, lib) {
  var fn = {}, lib = new lib();
  $.extend(fn, {
    timeInterVal: null,
    init: function(){
      var _t = this;
      _t.bindEvent();
    },
    bindEvent: function(){
      var _t = this;
      /*监测输入框*/
      $("input[name=userTel]").keyup(function(){
        if($(this).val().trim().length > 0 && !$(".ui-send-code").hasClass('ui-ing')){
          $(".ui-send-code").removeClass("ui-time-show");
        }else{
          $(".ui-send-code").addClass("ui-time-show");
        }
      });
      $("input[name=userCode]").keyup(function(){
        if($(this).val().trim().length > 0 && !$(".ui-red-botton").hasClass('ui-ing')){
          $(".ui-red-botton").removeClass("ui-disable");
        }else{
          $(".ui-red-botton").addClass("ui-disable");
        }
      });
      /*点击发送验证码*/
      $(document).on("tap", ".ui-send-code:not(.ui-time-show)", function(){
        var _this = this;
        $(_this).addClass("ui-ing ui-time-show");
        if(!_t.checkTel($("input[name=userTel]").val())){
          lib.showMessage("请输入正确的手机号");
          $(_this).removeClass("ui-ing ui-time-show");
          return;
        }
        lib.ajx(lib.getReq().ser + '/verifyCode/sendSmsCode.action', {phone: $("input[name=userTel]").val()}, function(data){
          lib.showMessage(data.info);
          var time = 120;
          _t.timeInterVal = null;
          _t.timeInterVal = setInterval(function(){
            if( --time < 1){
              $(_this).text('发送验证码');
              $(_this).removeClass("ui-ing ui-time-show");
              clearInterval(_t.timeInterVal);
              return;
            }
            $(_this).text(time + 's"');
          }, 1000);
          
        }, function(){
          lib.showMessage("网络超时，请稍后再试");
          $(_this).removeClass("ui-ing ui-time-show");
        });
      });
      
      /*点击登陆按钮*/
      $(document).on("tap", ".ui-red-botton:not(.ui-disable)", function(data){
        var _this = this;
        $(_this).addClass("ui-ing ui-disable");
        if(!_t.checkTel($("input[name=userTel]").val())){
          lib.showMessage("请输入正确的手机号");
          $(_this).removeClass("ui-ing ui-disable");
          return;
        }
        if($("input[name=userCode]").val().trim().length === 0){
          lib.showMessage("请输入短信验证码");
          $(_this).removeClass("ui-ing ui-disable");
          return;
        }
        if($("input[name=userPass]").val().trim().length < 6){
          lib.showMessage("请输入您的新密码,长度至少6位");
          $(_this).removeClass("ui-ing ui-disable");
          return;
        }
        lib.onLoading();
        var partnerId = $.cookie('partner_id') ? $.cookie('partner_id') : '4';
        lib.ajx(lib.getReq().ser + '/user/getbackPass.action', {username: $("input[name=userTel]").val(), smsCode: $("input[name=userCode]").val(), newPass: $("input[name=userPass]").val()}, function(data){
          if(data.infocode === "0"){
            lib.showMessage("密码设置成功");
            setTimeout(function(){
              location.href = "/seller/login.html"
            }, 1000);
          }else{
            lib.showMessage(data.info);
            $(_this).removeClass("ui-ing ui-disable");
          }
          
        }, function(){
          lib.showMessage("网络超时，请稍后再试");
          $(_this).removeClass("ui-ing ui-disable");
        });
      });
    },
    checkTel: function(num){
      var numReg = /^1[34578]\d{9}$/;
      return numReg.test(num);
    }
  });
  
  fn.init();
});